home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / toolkit.jar / content / global / bindings / dialog.xml < prev    next >
Encoding:
Extensible Markup Language  |  2002-05-14  |  11.5 KB  |  326 lines

  1. <?xml version="1.0"?>
  2.  
  3. <bindings id="dialogBindings"
  4.           xmlns="http://www.mozilla.org/xbl"
  5.           xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  6.           xmlns:xbl="http://www.mozilla.org/xbl">
  7.  
  8.   <binding id="dialog-base">
  9.     <resources>
  10.       <stylesheet src="chrome://global/skin/dialog.css"/>
  11.     </resources>
  12.   </binding>
  13.   
  14.   <binding id="dialog" extends="chrome://global/content/bindings/dialog.xml#dialog-base">
  15.     <content>
  16.       <xul:vbox class="box-inherit dialog-content-box" flex="1">
  17.         <children/>
  18.       </xul:vbox>
  19.           
  20.       <xul:hbox class="dialog-button-box" pack="end"
  21.                 xbl:inherits="pack=buttonpack,align=buttonalign,dir=buttondir,orient=buttonorient">
  22.         <xul:button dlgtype="accept" class="dialog-button"/>
  23.         <xul:button dlgtype="extra1" class="dialog-button" hidden="true" label=""/>
  24.         <xul:button dlgtype="extra2" class="dialog-button" hidden="true" label=""/>
  25.         <xul:button dlgtype="cancel" class="dialog-button"/>
  26.         <xul:button dlgtype="help" class="dialog-button" hidden="true"/>
  27.         <xul:button dlgtype="disclosure" class="dialog-button" hidden="true"/>
  28.       </xul:hbox>
  29.     </content>
  30.  
  31.     <implementation>
  32.       <field name="_mStrBundle">null</field>
  33.       <field name="_closeHandler">(function(event) {
  34.         if (!document.documentElement.cancelDialog())
  35.           event.preventDefault();
  36.       })</field>
  37.       <field name="enterDefaultAlways">false</field>
  38.  
  39.       <property name="buttons"
  40.                 onget="return this.getAttribute('buttons');"
  41.                 onset="this._configureButtons(val); return val;"/>
  42.  
  43.       <method name="acceptDialog">
  44.         <body>
  45.         <![CDATA[
  46.           return this._doButtonCommand("accept");
  47.         ]]>
  48.         </body>
  49.       </method>
  50.       
  51.       <method name="cancelDialog">
  52.         <body>
  53.         <![CDATA[
  54.           return this._doButtonCommand("cancel");
  55.         ]]>
  56.         </body>
  57.       </method>
  58.       
  59.       <method name="getButton">
  60.         <parameter name="aDlgType"/>
  61.         <body>
  62.         <![CDATA[
  63.           return this._buttons[aDlgType];
  64.         ]]>
  65.         </body>
  66.       </method>
  67.  
  68.       <method name="moveToAlertPosition">
  69.         <body>
  70.         <![CDATA[
  71.           // hack. we need this so the window has something like its final size
  72.           if (window.outerWidth == 1) {
  73.             dump("Trying to position a sizeless window; caller should have called sizeToContent() or sizeTo(). See bug 75649.\n");
  74.             sizeToContent();
  75.           }
  76.  
  77.           var xOffset = (opener.outerWidth - window.outerWidth) / 2;
  78.           var yOffset = opener.outerHeight / 5;
  79.  
  80.           var newX = opener.screenX + xOffset;
  81.           var newY = opener.screenY + yOffset;
  82.  
  83.           // ensure the window is fully onscreen (if smaller than the screen)
  84.           if (newX < screen.availLeft)
  85.             newX = screen.availLeft + 20;
  86.           if ((newX + window.outerWidth) > (screen.availLeft + screen.availWidth))
  87.             newX = (screen.availLeft + screen.availWidth) - window.outerWidth - 20;
  88.  
  89.           if (newY < screen.availTop)
  90.             newY = screen.availTop + 20;
  91.           if ((newY + window.outerHeight) > (screen.availTop + screen.availHeight))
  92.             newY = (screen.availTop + screen.availHeight) - window.outerHeight - 60;
  93.  
  94.           window.moveTo( newX, newY );
  95.         ]]>
  96.         </body>
  97.       </method>
  98.  
  99.       <method name="centerWindowOnScreen">
  100.         <body>
  101.         <![CDATA[
  102.           var xOffset = screen.availWidth/2 - window.outerWidth/2;
  103.           var yOffset = screen.availHeight/2 - window.outerHeight/2; //(opener.outerHeight *2)/10;
  104.   
  105.           xOffset = xOffset > 0 ? xOffset : 0;
  106.           yOffset = yOffset > 0 ? yOffset : 0;
  107.           window.moveTo(xOffset, yOffset);
  108.         ]]>
  109.         </body>
  110.       </method>
  111.  
  112.       <constructor>
  113.       <![CDATA[
  114.         this._configureButtons(this.getAttribute("buttons"));
  115.  
  116.         // listen for when window is closed via native close buttons
  117.         window.addEventListener("close", this._closeHandler, false);
  118.  
  119.         // for things that we need to initialize after onload fires
  120.         window.addEventListener("load", this.postLoadInit, false);
  121.  
  122.         window.moveToAlertPosition = this.moveToAlertPosition;
  123.         window.centerWindowOnScreen = this.centerWindowOnScreen;
  124.       ]]>
  125.       </constructor>
  126.  
  127.       <method name="postLoadInit">
  128.         <parameter name="aEvent"/>
  129.         <body>
  130.         <![CDATA[
  131.           var focusInit = 
  132.             function() {
  133.               // give focus to the first focusable element in the dialog
  134.               if (!document.commandDispatcher.focusedElement)
  135.                 document.commandDispatcher.advanceFocusIntoSubtree(document.documentElement);
  136.             };
  137.  
  138.           // Give focus after onload completes, see bug 103197.
  139.           setTimeout(focusInit, 0);
  140.         ]]>
  141.         </body>
  142.       </method>                
  143.  
  144.       <property name="mStrBundle">
  145.         <getter>
  146.         <![CDATA[
  147.           if (!this._mStrBundle) {
  148.             // need to create string bundle manually instead of using <xul:stringbundle/>
  149.             // see bug 63370 for details
  150.             var localeService = Components.classes["@mozilla.org/intl/nslocaleservice;1"]
  151.                                   .getService(Components.interfaces.nsILocaleService);
  152.             var stringBundleService = Components.classes["@mozilla.org/intl/stringbundle;1"]
  153.                                   .getService(Components.interfaces.nsIStringBundleService);
  154.             var bundleURL = "chrome://global/locale/dialog.properties";
  155.             this._mStrBundle = stringBundleService.createBundle(bundleURL, localeService.GetApplicationLocale());
  156.           }
  157.           return this._mStrBundle;
  158.         ]]></getter>
  159.       </property>
  160.       
  161.       <method name="_configureButtons">
  162.         <parameter name="aButtons"/>
  163.         <body>
  164.         <![CDATA[
  165.           // by default, get all the anonymous button elements
  166.           var buttons = {};
  167.           this._buttons = buttons;
  168.           buttons.accept = document.getAnonymousElementByAttribute(this, "dlgtype", "accept");
  169.           buttons.cancel = document.getAnonymousElementByAttribute(this, "dlgtype", "cancel");
  170.           buttons.extra1 = document.getAnonymousElementByAttribute(this, "dlgtype", "extra1");
  171.           buttons.extra2 = document.getAnonymousElementByAttribute(this, "dlgtype", "extra2");
  172.           buttons.help = document.getAnonymousElementByAttribute(this, "dlgtype", "help");
  173.           buttons.disclosure = document.getAnonymousElementByAttribute(this, "dlgtype", "disclosure");
  174.  
  175.           // look for any overriding explicit button elements
  176.           var exBtns = this.getElementsByAttribute("dlgtype", "*");
  177.           var dlgtype;
  178.           var i;
  179.           for (i = 0; i < exBtns.length; ++i) {
  180.             dlgtype = exBtns[i].getAttribute("dlgtype");
  181.             buttons[dlgtype].hidden = true; // hide the anonymous button
  182.             buttons[dlgtype] = exBtns[i];
  183.           }
  184.  
  185.           // add the label and oncommand handler to each button
  186.           for (dlgtype in buttons) {
  187.             var button = buttons[dlgtype];
  188.             buttons[dlgtype].addEventListener("command", this._handleButtonCommand, true);
  189.             // don't override custom labels with pre-defined labels on explicit buttons
  190.             if (!button.hasAttribute("label"))
  191.               button.setAttribute("label", this.mStrBundle.GetStringFromName("button-"+dlgtype));
  192.           }
  193.           
  194.           // ensure that hitting enter triggers ondialogaccept
  195.           buttons["accept"].setAttribute("default", "true");
  196.           
  197.           // if there is a special button configuration, use it
  198.           if (aButtons) {
  199.             // expect a comma delimitd list of dlgtype values
  200.             var list = aButtons.split(",");
  201.   
  202.             // mark shown dlgtypes as true
  203.             var shown = { accept: false, cancel: false, help: false,
  204.                           disclosure: false, extra1: false, extra2: false };
  205.             for (i = 0; i < list.length; ++i)
  206.               shown[list[i].replace(/ /g, "")] = true;
  207.             
  208.             // hide/show the buttons we want
  209.             for (dlgtype in shown) {
  210.               if (shown[dlgtype])
  211.                 buttons[dlgtype].hidden = false;
  212.               else
  213.                 buttons[dlgtype].hidden = true;
  214.             }
  215.           }
  216.         ]]>
  217.         </body>
  218.       </method>
  219.       
  220.       <method name="_handleButtonCommand">
  221.         <parameter name="aEvent"/>
  222.         <body>
  223.         <![CDATA[
  224.           return document.documentElement._doButtonCommand(
  225.                                         aEvent.target.getAttribute("dlgtype"));
  226.         ]]>
  227.         </body>
  228.       </method>
  229.       
  230.       <method name="_doButtonCommand">
  231.         <parameter name="aDlgType"/>
  232.         <body>
  233.         <![CDATA[
  234.           // calling window.close() while an oncommand event
  235.           // call is on the stack fails to close the window, 
  236.           // so we need to do this ugly setTimeout hack
  237.           window.setTimeout(
  238.             function(aDlgType) {
  239.               document.documentElement._reallyDoButtonCommand(aDlgType);
  240.             },
  241.             0, aDlgType);
  242.         ]]>
  243.         </body>
  244.       </method>
  245.       
  246.       <method name="_reallyDoButtonCommand">
  247.         <parameter name="aDlgType"/>
  248.         <body>
  249.         <![CDATA[
  250.           var button = this.getButton(aDlgType);
  251.           if (!button.disabled) {
  252.             var noCancel = this._fireButtonEvent(aDlgType);
  253.             if (noCancel) {
  254.               if (aDlgType == "accept" || aDlgType == "cancel")
  255.                 window.close();
  256.             }
  257.             return noCancel;
  258.           }
  259.           return true;
  260.         ]]>
  261.         </body>
  262.       </method>
  263.       
  264.       <method name="_fireButtonEvent">
  265.         <parameter name="aDlgType"/>
  266.         <body>
  267.         <![CDATA[
  268.           var event = document.createEvent("Events");
  269.           event.initEvent("dialog"+aDlgType, false, true);
  270.           
  271.           // handle dom event handlers
  272.           var noCancel = this.dispatchEvent(event);
  273.           
  274.           // handle any xml attribute event handlers
  275.           var handler = this.getAttribute("ondialog"+aDlgType);
  276.           if (handler != "") {
  277.             var fn = new Function("event", handler);
  278.             var returned = fn(event);
  279.             if (returned == false)
  280.               noCancel = false;
  281.           }
  282.           
  283.           return noCancel;
  284.         ]]>
  285.         </body>
  286.       </method>
  287.  
  288.       <method name="_hitEnter">
  289.         <body>
  290.         <![CDATA[
  291.           // if a button is focused, do nothing, so that activating the button 
  292.           // doesn't cause the dialog to exit
  293.           if (!this.enterDefaultAlways) {
  294.             var focused = document.commandDispatcher.focusedElement;
  295.             if (focused && focused.localName == "button")
  296.               return;
  297.           }
  298.  
  299.           // only accept dialog if accept button is the default
  300.           var btn = this.getButton("accept");
  301.           if (btn && btn.hasAttribute("default"))
  302.             this.acceptDialog();
  303.         ]]>
  304.         </body>
  305.       </method>
  306.  
  307.     </implementation>
  308.     
  309.     <handlers>
  310.       <handler event="keypress" keycode="VK_ENTER" action="this._hitEnter();"/>
  311.       <handler event="keypress" keycode="VK_RETURN" action="this._hitEnter();"/>
  312.       <handler event="keypress" keycode="VK_ESCAPE" action="this.cancelDialog();"/>
  313.     </handlers>
  314.  
  315.   </binding>
  316.  
  317.   <binding id="dialogheader" extends="chrome://global/content/bindings/dialog.xml#dialog-base">
  318.     <content>
  319.       <xul:label class="dialogheader-title" xbl:inherits="value=title"/>
  320.       <xul:spacer flex="1"/>
  321.       <xul:label class="dialogheader-description" xbl:inherits="value=description"/>
  322.     </content>
  323.   </binding>
  324.  
  325. </bindings>
  326.